package tazadum.engine;

public class TimeProvider {
	private long t0;
	private long lastFrame;
	private float deltaTime;
	private float adjustedTime;
	private float fps;

	public TimeProvider() {
		t0 = time();
		lastFrame = t0;
		fps = 20;
	}

	public void timeZero() {
		t0 = time();
		lastFrame = t0;
	}

	public void newFrame() {
		final long t = time();
		deltaTime = (t - lastFrame) * 0.001f;
		adjustedTime = (t - t0) * 0.001f;
		if (deltaTime > 0.1f) {
			deltaTime = 0.1f;
		}
		if (deltaTime > 0) {
			fps = 0.5f * fps + 0.5f * 1f / deltaTime;
		}
		lastFrame = t;
	}

	public float getFrameDeltaTime() {
		return deltaTime;
	}

	public float getFrameTime() {
		return adjustedTime;
	}

	public long getTime() {
		return time() - t0;
	}

	private long time() {
		return System.currentTimeMillis();
	}

	public float getFps() {
		return fps;
	}

	public void adjustTime(final float time) {
		System.out.println("time += " + time);
		t0 -= (long) (time * 1000);
	}
}
